package utils

import (
	"fmt"
	"log"
	"lottery_code/comm"
	"lottery_code/datasource"
	"math"
	"time"
)

const userFrameSize = 2

func init(){
	resetGroupUserList()
}

func resetGroupUserList(){
	log.Println("user_day.resetGroupUserList start")
	cacheObj:=datasource.InstanceCache()
	for i:=0;i<userFrameSize;i++{
		key:=fmt.Sprintf("day_users__%d",i)
		cacheObj.Do("DEL",key)
	}
	log.Println("user_day.resetGroupUserList end")
	//零点归零定时器
	duration:=comm.NextDayDuration()
	time.AfterFunc(duration,resetGroupIpList)
}


func IncrUserLuckyNum(uid int) int64{
	i:=uid%userFrameSize
	key:=fmt.Sprintf("day_users_%d",i)
	cacheObj:=datasource.InstanceCache()
	rs,err:=cacheObj.Do("HINCRBY",key,uid,1)
	if err!=nil{
		log.Println("user_day_lucky rediis HINCRBY key=",key,",err=",err)
		return math.MaxInt32
	}else{
		return rs.(int64)
	}
}

func InitUserLuckyNum(uid int,num int64){
	i:=uid%userFrameSize
	key:=fmt.Sprintf("day_users_%d",i)
	cacheObj:=datasource.InstanceCache()
	_,err:=cacheObj.Do("HSET",key,uid,num)
	if err!=nil{
		log.Println("user_day_lucky redis HSET key=",key,",uid=",uid,
			",err=",err)
	}
}